package com.carter.springframework.core.io;

import cn.hutool.core.lang.Assert;
import com.carter.springframework.util.ClassUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ClasspathResource implements Resource{
    private String path;

    private ClassLoader classLoader;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path == null ? null : path.trim();
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public ClasspathResource(String path) {
        this(path, null);
    }

    public ClasspathResource(String path, ClassLoader classLoader) {
        Assert.notNull(path);
        this.path = path;
        this.classLoader = null == classLoader ? ClassUtils.getDefaultClassLoader() : classLoader;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream is = classLoader.getResourceAsStream(path.substring("classpath:".length()));
        if (null==is){
            throw new FileNotFoundException(path + "文件未找到");
        }
        return null;
    }

    @Override
    public String getDescription() {
        return null;
    }
}
